﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GalaxyPresident
{
    class PlanetView : VisibleGroup
    {
        protected static Texture2D OCEAN_TEXTURE=
            Constant.content.Load<Texture2D>(@"Resources/Ocean");
        protected static Texture2D LIVE_LAND_TEXTURE=
            Constant.content.Load<Texture2D>(@"Resources/LandLivePlanet");
        protected static Texture2D LIVE_ATMOSPHERE_TEXTURE =
            Constant.content.Load<Texture2D>(@"Resources/AtmosphereLivePlanet");
        protected static Texture2D DEATH_LAND_TEXTURE =
            Constant.content.Load<Texture2D>(@"Resources/LandDeathPlanet");
        protected static Texture2D DEATH_ATMOSPHERE_TEXTURE =
            Constant.content.Load<Texture2D>(@"Resources/AtmosphereDeathPlanet");
        Planet planet;
        InfomationType showInfo = InfomationType.Brief;
        VisibleObject circleSprite;
        public InfomationType ShowInfo
        {
            get { return showInfo; }
            set { showInfo = value; }
        }
        public Planet Planet
        {
            get { return planet; }
            set { planet = value; }
        }
        Sprite spriteOcean;
        MutiSpriteObject planetObject;
        public PlanetView(Planet planet)
        {
            this.planet = planet;
            planetObject = new MutiSpriteObject();
            spriteOcean = new Sprite(OCEAN_TEXTURE, 1, 1);
            planetObject.Scale = planet.Diameter / spriteOcean.Width;
            spriteOcean.SetOriginPosition(ORIGIN_POSITION.Center);
            planetObject.Depth = 0.11f;

            spriteOcean.Color = planet.BackColor;
            planetObject.AddSprite(spriteOcean);

            Sprite spriteLand;
            if (planet.GetType() == typeof(LivalePlanet))
            {
                spriteLand = new Sprite(LIVE_LAND_TEXTURE, 1, 1);
            }
            else
            {
                spriteLand = new Sprite(DEATH_LAND_TEXTURE, 1, 1);
            }
            spriteLand.SetOriginPosition(ORIGIN_POSITION.Center);
            spriteLand.Color = planet.LandColor;
            spriteLand.Rotate = (float)(Constant.random.NextDouble() * Math.PI * 2);
            planetObject.AddSprite(spriteLand);

            Sprite spriteAtmosphere;
            if (planet.GetType() == typeof(LivalePlanet))
            {
                spriteAtmosphere = new Sprite(LIVE_ATMOSPHERE_TEXTURE, 1, 1);
            }
            else
            {
                spriteAtmosphere = new Sprite(DEATH_ATMOSPHERE_TEXTURE, 1, 1);
            }
            spriteAtmosphere.SetOriginPosition(ORIGIN_POSITION.Center);
            spriteAtmosphere.Color = planet.AtmospherColor;
            planetObject.AddSprite(spriteAtmosphere);
            
            AddChild(planetObject);
            foreach (Moon moon in planet.Moons)
            {
                AddChild(new MoonView(moon));
            }
            Position = planet.Position;

            circleSprite = new VisibleObject(@"Resources/Ingame/circle", 4, 6);
            circleSprite.Depth = 0.11f;
            circleSprite.Sprite.Delay = 80;
            circleSprite.Sprite.SetOriginPosition(ORIGIN_POSITION.Center);
            circleSprite.Scale = planetObject.Scale;
            AddChild(circleSprite);
        }
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            Position = Planet.Position;
            if (ShowInfo != InfomationType.None)
            {
                circleSprite.Visible = true;
                if (planet.TeamOwner != null)
                {
                    circleSprite.Color = planet.TeamOwner.TeamColor;
                }
                else
                {
                    circleSprite.Color = Color.White;
                }
            }
            else
            {
                circleSprite.Visible = false;
            }
        }
        public Rectangle getBound()
        {
            return spriteOcean.GetBound(planetObject.GetDrawPosition(), planetObject.GetDrawScale());
        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);
            Vector2 drawPosition = GetDrawPosition();
            if (ShowInfo == InfomationType.Brief)
            {
                Text.Draw(spriteBatch, planet.Name, Constant.fontSmall,
                    drawPosition.X + 30 * GetDrawScale(), drawPosition.Y - 30 * GetDrawScale());
            }
            else if (ShowInfo == InfomationType.Full)
            {
                Text.Draw(spriteBatch, planet.Name, Constant.fontNormalBold,
                    drawPosition.X + 30 * GetDrawScale(), drawPosition.Y - 30 * GetDrawScale());
                //spriteBatch.DrawString(Constant.fontSmall, SolarSystem.Name,
                //  new Vector2(position.X + 60 * GetDrawScale(), position.Y - 60 * GetDrawScale()+60), Color.White);
                /*spriteBatch.DrawString(Constant.fontSmall, planet.Position.ToString(),
                    new Vector2(drawPosition.X + 30 * GetDrawScale(), drawPosition.Y - 30 * GetDrawScale() + 30), Color.White);*/
                
            }
        }
    }
}
